﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SharingCenter.Web.Classes;
using SharingCenter.Web.Classes.enums;
using SharingCenter.Web.Classes.Groups;
using System.Xml.Linq;
using SharingCenter.Web.Classes.SiteEvents;

namespace SharingCenter.Web.Mod.Groups
{
    public partial class CreateThread : GroupsPageBase
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            
            ParseGroupIdFromQueryString();
            HasRights(groupId, Profile.UserId, true);

            InsertButton.Click += new EventHandler(InsertButton_Click);
            SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.SiteMap_SiteMapResolve);
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(this.SiteMap_SiteMapResolve);
        }

        private SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
        {
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            SiteMapNode tmpNode = currentNode;
            if (null != (tmpNode = tmpNode.ParentNode))
            {
                tmpNode.Url += "?id=" + groupId;
            }

            if (null != (tmpNode = tmpNode.ParentNode))
            {
                tmpNode.Title = group.GroupName;
                tmpNode.Url += "?id=" + groupId;
            }
            return currentNode;
        }

        void Page_Load(object sender, EventArgs e)
        {
            ListView1.DataSource = WebConsts.POST_ICON_LIST;
            ListView1.DataBind();
            imgDefaultIcon.AlternateText = WebConsts.POST_ICON_LIST.First().Value;
            imgDefaultIcon.ToolTip = imgDefaultIcon.AlternateText;
            imgDefaultIcon.ImageUrl = string.Format("{0}/{1}", WebConsts.POST_ICON_DIR, WebConsts.POST_ICON_LIST.First().Key);
        }

        void InsertButton_Click(object sender, EventArgs e)
        {
            Sc_PostThread thread;
            Sc_Post post;
            DoCreateThread(out thread, out post);
            RaiseEvent(ref thread, ref post);
            RedirectToPostPage(thread.ThreadId, true);
        }

        void DoCreateThread(out Sc_PostThread thread, out Sc_Post post)
        {
            Guid cid = (Guid)dataContext.GetGroup(groupId).First().PostCategoryId;
            PostType type = (PostType)int.Parse(rdoType.SelectedValue);
            thread = PostOperations.CreateThread(Profile.UserId, cid, type, PostIcon.Value, false);
            post = PostOperations.CreatePost(thread.ThreadId, Profile.UserId, txtTitle.Text, Editor1.Content, type, Profile.Talks.Signature, true);
        }

        private void RaiseEvent(ref Sc_PostThread thread, ref Sc_Post post)
        {
            XElement context = new XElement("Event",
                    new XElement("Title", post.PostTitle),
                    new XElement("ThreadId", thread.ThreadId),
                    new XElement("GroupId", groupId.ToString()),
                    new XElement("CreatorUserId", Profile.UserId));

            SiteEventOperations.AddEvent(appName, "ThreadCreated", appFolder, Profile.UserId, context, true, groupId);
        }
    }
}
